package com.example.yt_app.Activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.constraintlayout.solver.PriorityGoalRow;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.example.yt_app.Activity.BaseActivity.BaseActivity;
import com.example.yt_app.AppManager;
import com.example.yt_app.Fragment.LoginFragment.PasswordFragment;
import com.example.yt_app.Fragment.LoginFragment.PhoneFragment;

import com.example.yt_app.R;
import com.example.yt_app.Urils.sample.CustomViewPager;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;

import butterknife.BindView;

/**
 *
 * @author dell
 * @date 2021/7/15
 */
@SuppressWarnings("ALL")
public class Login extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener{

    @BindView(R.id.viewPager)
    CustomViewPager viewPager;
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @BindView(R.id.zhenghao_login)
    LinearLayout zhenghao_login;
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @BindView(R.id.phone_login)
    LinearLayout phone_login;
    @BindView(R.id.V1)
    View v1;
    @BindView(R.id.V2)
    View v2;
    private ArrayList<Fragment> mFragments;

    private long mFirstPressedTime;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_fragment_login;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        //隐藏状态栏
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init();

    }

    @Override
    protected void initData() {
        mFragments = new ArrayList<>();
        PasswordFragment passwordFragment = new PasswordFragment();
        PhoneFragment phoneFragment = new PhoneFragment();

        mFragments.add(passwordFragment);
        mFragments.add(phoneFragment);

    }

    @Override
    protected void initView() {
        AppManager.getInstance().addActivity(this);
        requestPower();
        viewPager.setAdapter(new Login.MyAdapter(getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(2);
        zhenghao_login.setSelected(true);



    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        AppManager.getInstance().removeActivity(this);
    }
    @Override
    protected void setListener() {
        zhenghao_login.setOnClickListener(this);
        phone_login.setOnClickListener(this);
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhenghao_login:
                viewPager.setCurrentItem(1);
                tabSelected(zhenghao_login);
                v1.setBackground(getDrawable(R.drawable.botton_off));
                v2.setBackground(getDrawable(R.drawable.botttom_et));
                break;
            case R.id.phone_login:
                viewPager.setCurrentItem(0);
                tabSelected(zhenghao_login);
                v1.setBackground(getDrawable(R.drawable.botttom_et));
                v2.setBackground(getDrawable(R.drawable.botton_off));

                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                tabSelected(zhenghao_login);
                v1.setBackground(getDrawable(R.drawable.botttom_et));
                v2.setBackground(getDrawable(R.drawable.botton_off));
                break;
            case 1:
                tabSelected(phone_login);
                v1.setBackground(getDrawable(R.drawable.botton_off));
                v2.setBackground(getDrawable(R.drawable.botttom_et));
                break;

            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void tabSelected(LinearLayout linearLayout) {

        zhenghao_login.setSelected(false);
        phone_login.setSelected(false);
        linearLayout.setSelected(true);
    }

    @SuppressWarnings("AliDeprecation")
    private class MyAdapter extends FragmentPagerAdapter {
        @SuppressWarnings("AliDeprecation")
        MyAdapter(FragmentManager fm) {
            //noinspection AliDeprecation
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
    /**
     *  动态赋予权限
     * */
    public void requestPower() {
        //判断是否已经赋予权限
        //如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true。
    


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            //申请权限，字符串数组内是一个或多个要申请的权限，1是申请权限结果的返回参数，在onRequestPermissionsResult可以得知申请结果
        {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
                //这里可以写个对话框之类的项向用户解释为什么要申请权限，并在对话框的确认键后续再次申请权限.它在用户选择"不再询问"的情况下返回false
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            }
        }

    }
    //声明一个long类型变量：用于存放上一点击“返回键”的时刻
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    private long mExitTime;
    @SuppressWarnings("AlibabaUndefineMagicConstant")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            //noinspection AlibabaUndefineMagicConstant
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                showMessage("再按一次退出程序");

                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
